/*
 * Copyright (c) 2016. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.techindata.cacheredis;

import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Tuple;

import java.util.Set;

/**
 * <p>
 * Created by yanshen@tsinghuabigdata.com on 2016/2/23.
 * </p>
 *
 * @author yanshen@tsinghuabigdata.com
 * @version V1.0
 * @packageName: com.tsinghuabigdata.cacheredis
 * @createTime: 2016/2/23 16:21
 */
public class CacheZSet {
    static Logger logger = Logger.getLogger(CacheZSet.class.getName());

    /**
     * 插入ZSet
     *
     * @param key
     * @param value
     * @return
     */
    public static long put(String key, String value, double score) {
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.zadd(key, score, value);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1;
        } finally {
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 获取ZSet有序集合的数目
     *
     * @param key
     * @return
     */
    public static long zcard(String key) {
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.zcard(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1;
        } finally {
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 通过索引区间正序获取key值下的数据,如果 min=0，max = -1表示全部
     *
     * @param key 传入的key值
     * @param min 索引最小值
     * @param max 索引最大值
     * @return String 类型
     */
    public static Set<String> zrange(String key, int min, int max) {
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.zrange(key, min, max);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        } finally {
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }

    /**
     * 通过索引区间倒序获取key值下的数据,如果 min=0，max = -1表示全部
     *
     * @param key 传入的key值
     * @param min 索引最小值
     * @param max 索引最大值
     * @return String 类型
     */
    public static Set<String> zrevrange(String key, int min, int max) {
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.zrevrange(key, min, max);
            return jedis.zrevrange(key, min, max);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        } finally {
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }

    /**
     * 通过索引区间倒序获取key值下的数据,如果 min=0，max = -1表示全部
     *
     * 返回数据：value+score
     *
     * @param key 传入的key值
     * @param min 索引最小值
     * @param max 索引最大值
     * @return String 类型
     */
    public static Set<Tuple> zrevrangeWithScores(String key, int min, int max) {
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.zrevrangeWithScores(key, min, max);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        } finally {
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 通过索引区间倒正序获取key值下的数据,如果 min=0，max = -1表示全部
     *
     * 返回数据：value+score
     *
     * @param key 传入的key值
     * @param min 索引最小值
     * @param max 索引最大值
     * @return String 类型
     */
    public static Set<Tuple> zrangeWithScores(String key, int min, int max) {
    	JedisPool pool = RedisConnectionPool.getPool();
    	Jedis jedis = null;
    	try {
    		jedis = pool.getResource();
    		return jedis.zrangeWithScores(key, min, max);
    	} catch (Exception e) {
    		logger.error(e.getMessage(), e);
    		return null;
    	} finally {
    		RedisConnectionPool.returnResource(pool, jedis);
    	}
    }


    /**
     * 移除有序集key中的一个或多个成员，不存在的成员将被忽略。
     *
     * @param key    传入的key值
     * @param member 成员
     * @return long 类型
     */
    public static long zrem(String key, String... member) {
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.zrem(key, member);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1;
        } finally {
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     *  删除一个key值指定索引范围数据,如果 start=0，end = -1表示全部
     * @param key 传入的key值
     * @param start 开始的索引数
     * @param end 结束的索引数
     * @return List<String> 类型
     */
    public static long zremrangeByRank(String key, long start,long end) {
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.zremrangeByRank(key,start,end);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1;
        } finally {
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 修改排序的分数值
     * @param key
     * @param score
     * @param member
     * @return
     */
    public static double zincrby(String key, double score, String member) {
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.zincrby(key, score, member);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1;
        } finally {
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 
     * @Description: 返回有序集中，成员的分数值   
     * @return:  
     * @date:   2019年7月31日 上午11:55:08  
     * @author: wangshunyi    
     * @throws
     */
    public static double zscore(String key,String member) {
    	JedisPool pool = RedisConnectionPool.getPool();
    	Jedis jedis = null;
    	try {
    		jedis = pool.getResource();
    		return jedis.zscore(key, member);
    	} catch (Exception e) {
    		logger.error(e.getMessage(), e);
    		return -1;
    	} finally {
    		RedisConnectionPool.returnResource(pool, jedis);
    	}
    }
}
